<%@page import="com.boxin.ims.modules.momarketing.service.QRCodeService"%>
<%@page import="com.boxin.ims.modules.momarketing.common.QRCodeUtils"%>
<%@page import="java.io.File"%>
<%@page import="com.thinkgem.jeesite.common.utils.FileUtils"%>
<%@page import="com.boxin.ims.modules.wechat.utils.WechatUtils"%>
<%@page import="com.boxin.ims.modules.momarketing.entity.QRCode"%>
<%@page import="com.thinkgem.jeesite.common.utils.StringUtils"%>
<%@page import="com.boxin.ims.modules.wechat.service.WechatMemberService"%>
<%@page import="com.boxin.ims.modules.wechat.entity.WechatMember"%>
<%@page import="com.boxin.ims.modules.recharge.entity.Recharge"%>
<%@page import="com.boxin.ims.modules.recharge.service.RechargeService"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="javax.sql.DataSource"%>
<%@page import="com.boxin.ims.modules.wechat.utils.SpringUtils"%>
<%@page import="com.boxin.framework.base.dao.DaoHelper"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
  <%
  
  	String uid = request.getParameter("uid");
    
    if(StringUtils.isEmpty(uid)){
    	
    	out.print("<h3>缺少 uid参数</h3>");
    
    }else{
    	QRCodeService qrCodeService = (QRCodeService)  SpringUtils.getBean("QRCodeService");
    	DaoHelper daoHelper = (DaoHelper)  SpringUtils.getBean("daoHelper");
      	DataSource ds = 	daoHelper.getSqlMapClientTemplate().getDataSource();
      	
      	
      	
      	RechargeService rechargeService = (RechargeService)  SpringUtils.getBean("rechargeService");
      	
      	WechatMemberService wechatMemberService = (WechatMemberService)  SpringUtils.getBean("wechatMemberService");
      	
      	try{
      		
      		String sql = "select * from wp_wechat_member  where user_id = "+uid;
      		Connection conn =  ds.getConnection();
      		PreparedStatement pstmt = conn.prepareStatement(sql);
      		
      		ResultSet rs = 	pstmt.executeQuery();
      		
      		while(rs.next()){
      			Long id = rs.getLong(1);
      			Long userId = rs.getLong("user_id");
      			String name = rs.getString("name");
      			//out.println(id+"\t"+name+"<br>");
      			String number = rs.getString("number");
      			
      			try{
      				
    	  			WechatMember member = wechatMemberService.get(id);
    	  			
    	  			Recharge recharge = rechargeService.getRechargeByUserIdAndNumber(userId, number);	
    	  			if(recharge != null){
    					recharge.setMoney(member.getMoney());
    					recharge.setName(name);
    					rechargeService.save(recharge);
    					out.print("更新会员卡:"+name+"\t"+number+"<br>");
    				}else{
    					//不存在则添加
    					recharge = new Recharge();
    					recharge.setMember(member);
    					recharge.setName(name);
    					recharge.setUser(member.getUser());
    					recharge.setMoney(member.getMoney());
    					recharge.setType("0");
    					recharge.setNumber(member.getNumber());
    					out.print("添加会员卡:"+name+"\t"+number+"<br>");
    					rechargeService.save(recharge);
    					
    					//二维码
    					QRCode qrcode = new QRCode();
    	  				String content = recharge.getMember().getNumber();
    	  				qrcode.setContent(content);
    	  				String path = WechatUtils.getMemberCardResourceSavePath(request);
    	  				FileUtils.createDirectory(path);
    	  				String filePath = path + File.separator+ content;
    	  				
    	  				QRCodeUtils.generateNoMarginQRCodePNG(content, filePath, 300,300);
    	  				qrcode.setFileName(content+".png");
    	  				qrcode.setPngPath(filePath+".png");
    	  				qrCodeService.save(qrcode);
    	  				
    	  				out.print(path);
    	  				recharge.setQrCode(qrcode);
    	  				
    	  				rechargeService.save(recharge);
    				}
    	  			
    	  			
    	  			
      			}catch(Exception e){
      				e.printStackTrace();
      			}
      		}
      		
      		rs.close();
      		conn.close();
      		
      		
      	}catch(Exception e){
      		e.printStackTrace();
      	}
    }
  	
  	
  	
  
  %>  
    
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>智讯互联</title>
</head>
<body>
	<h1>智讯互联</h1>
</body>
</html>